package com.ruoyi.web.controller.net;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.net.domain.DeviceStatusIsisNeighb;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.DeviceStatusPcc;
import com.ruoyi.net.service.IDeviceStatusPccService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 设备状态-PCC状态Controller
 * 
 * @author tony
 * @date 2024-12-11
 */
@Api(tags = "设备状态-PCC状态")
@RestController
@RequestMapping("/device/status/pcc")
public class DeviceStatusPccController extends BaseController
{
    @Autowired
    private IDeviceStatusPccService deviceStatusPccService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 查询设备状态-PCC状态列表
     */
    @ApiOperation(value = "读取设备状态-PCC状态列表", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:pcc:list')")
    @GetMapping("/read")
    public TableDataInfo read(DeviceStatusPcc deviceStatusPcc)
    {
        List<DeviceStatusPcc> deviceStatusPccs = new ArrayList<>();
        StringBuilder xml = new StringBuilder();
        xml.append("<filter type=\"subtree\">" +
                "<pcc-oper xmlns=\"http://fiberhome.com/fhnw/yang/pce/fos-pcc-oper\">" +
                "      <pcc-server-info>" +
                "        <server-ip-info>" +
//                "<server-ip>FHNMVPN</server-ip>" + // 查询 条件
//                " <is-best>interface</is-best>"+
//                " <is-connected>10.10.1.2</is-connected>"+
                "        </server-ip-info>" +
                "      </pcc-server-info>" +
                "</pcc-oper>" +
                "</filter>");
        NetElement netElement = netElementService.getCacheObject(deviceStatusPcc.getElementId());
        if (sysConfigService.selectDebugEnabled()) {
            deviceManager.getDevice(netElement.getIp());
            String rpcReply = deviceManager.getStatus(xml.toString());
            // 解析xml
            NodeList list = findNetConfXmlNodes(rpcReply, "//pcc-server-info/server-ip-info");
            if (list != null && list.getLength() > 0) {
                for (int i = 0; i < list.getLength(); i++) {
                    DeviceStatusPcc entity = new DeviceStatusPcc();
                    Node node = list.item(i);
                    // 通过标签名获取节点的值
                    entity.setIp(getXmlLabelValue(node, "server-ip"));
                    entity.setIsBest(getXmlLabelValue(node, "is-best"));
                    entity.setIsConnected(getXmlLabelValue(node, "is-connected"));

                    deviceStatusPccs.add(entity);
                }
            }
        }
        startPage();
        return getDIYPageTable(deviceStatusPccs);
    }

    /**
     * 查询设备状态-PCC状态列表
     */
    @ApiOperation(value = "查询设备状态-PCC状态列表", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:pcc:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceStatusPcc deviceStatusPcc)
    {
        startPage();
        List<DeviceStatusPcc> list = deviceStatusPccService.selectDeviceStatusPccList(deviceStatusPcc);
        return getDataTable(list);
    }

    /**
     * 导出设备状态-PCC状态列表
     */
    @ApiOperation(value = "导出设备状态-PCC状态列表", httpMethod = "POST")
//    @PreAuthorize("@ss.hasPermi('net:pcc:export')")
    @Log(title = "设备状态-PCC状态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceStatusPcc deviceStatusPcc)
    {
        List<DeviceStatusPcc> list = deviceStatusPccService.selectDeviceStatusPccList(deviceStatusPcc);
        ExcelUtil<DeviceStatusPcc> util = new ExcelUtil<DeviceStatusPcc>(DeviceStatusPcc.class);
        util.exportExcel(response, list, "设备状态-PCC状态数据");
    }

    /**
     * 获取设备状态-PCC状态详细信息
     */
    @ApiOperation(value = "获取设备状态-PCC状态详细信息", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:pcc:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deviceStatusPccService.selectDeviceStatusPccById(id));
    }

    /**
     * 新增设备状态-PCC状态
     */
    @ApiOperation(value = "新增设备状态-PCC状态", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:pcc:add')")
    @Log(title = "设备状态-PCC状态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceStatusPcc deviceStatusPcc)
    {
        return toAjax(deviceStatusPccService.insertDeviceStatusPcc(deviceStatusPcc));
    }

    /**
     * 修改设备状态-PCC状态
     */
    @ApiOperation(value = "修改设备状态-PCC状态", httpMethod = "PUT")
//    @PreAuthorize("@ss.hasPermi('net:pcc:edit')")
    @Log(title = "设备状态-PCC状态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceStatusPcc deviceStatusPcc)
    {
        return toAjax(deviceStatusPccService.updateDeviceStatusPcc(deviceStatusPcc));
    }

    /**
     * 删除设备状态-PCC状态
     */
    @ApiOperation(value = "删除设备状态-PCC状态", httpMethod = "DELETE")
//    @PreAuthorize("@ss.hasPermi('net:pcc:remove')")
    @Log(title = "设备状态-PCC状态", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceStatusPccService.deleteDeviceStatusPccByIds(ids));
    }
}
